package cn.xuqiudong.rpc.protocol.netty.handler;

import cn.xuqiudong.rpc.protocol.netty.codec.XqdDecoder;
import cn.xuqiudong.rpc.protocol.netty.codec.XqdEncoder;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.logging.LoggingHandler;

/**
 * 描述:
 * @author Vic.xu
 * @date 2022-02-21 9:39
 */
public class XqdClientInitializer extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel channel) {

        channel.pipeline()
                // 自定义长度解码器 , LineBasedFrameDecoder是基于换行符号
                .addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE,12,4,0,0))
                .addLast(new LoggingHandler())
                // 解码
                .addLast(new XqdDecoder())
                // 编码
                .addLast(new XqdEncoder())
                // 在管道中添加我们自己的接收数据实现方法
                .addLast(new XqdNettyClientHandler());
    }
}
